package com.atguigu.gmall.service.impl;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.cache.ShopCache;
import com.atguigu.gmall.mapper.BaseCategoryViewMapper;
import com.atguigu.gmall.service.BaseCategoryViewService;
import com.atguigu.gmall.entity.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 */
@Service
public class
BaseCategoryViewServiceImpl extends ServiceImpl<BaseCategoryViewMapper, BaseCategoryView> implements BaseCategoryViewService {

    @ShopCache(prefix = "indexCategoryInfo")
    @Override
    public  List<JSONObject> getIndexCategoryInfo() {
        List<BaseCategoryView> list = baseMapper.selectList(null);

        //第一层分类
        Map<String, List<BaseCategoryView>> collect = list.stream().collect(Collectors.groupingBy(p -> p.getCategory1Name()));
        //第一层遍历
        List<JSONObject> parentCategoryList  = new ArrayList<>();
        Integer index01 = 1;
        for (Map.Entry<String, List<BaseCategoryView>> stringListEntry : collect.entrySet()) {
            JSONObject category1  = new JSONObject();//创建第一层存储
            packageResult(stringListEntry, index01++, category1);//封装
            category1.put("categoryId",stringListEntry.getValue().get(0).getCategory1Id());
            //第二层分类
            Map<String, List<BaseCategoryView>> collect2 = stringListEntry.getValue().stream().collect(Collectors.groupingBy(p -> p.getCategory2Name()));
            //第二层遍历
            List<JSONObject> category1Children  = new ArrayList<>();
            Integer index02 = 1;
            for (Map.Entry<String, List<BaseCategoryView>> listEntry : collect2.entrySet()) {
                JSONObject category2 = new JSONObject();
                packageResult(listEntry, index02++, category2);
                category2.put("categoryId",stringListEntry.getValue().get(0).getCategory2Id());
                //第三层分类
                Map<String, List<BaseCategoryView>> collect3 = listEntry.getValue().stream().collect(Collectors.groupingBy(p -> p.getCategory3Name()));
                //第三层遍历
                ArrayList<JSONObject> category2Children = new ArrayList<>();
                Integer index03 = 1;
                for (Map.Entry<String, List<BaseCategoryView>> entry : collect3.entrySet()) {
                    JSONObject category3 = new JSONObject();
                    packageResult(entry, index03, category3);
                    category3.put("categoryId",stringListEntry.getValue().get(0).getCategory3Id());
                    category2Children.add(category3);
                    category2.put("categoryChild", category2Children);
                }
                category1Children .add(category2);
                category1.put("categoryChild", category1Children );
            }
            parentCategoryList .add(category1);
        }
        return parentCategoryList ;
    }
    public void packageResult(Map.Entry<String, List<BaseCategoryView>> entry, Integer index,JSONObject jsonObject) {
        try {
            jsonObject.put("index", index);
            jsonObject.put("categoryName", entry.getKey());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
